package com.woan.api.idao.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysRole;
import com.woan.api.vo.VadmininfoList;
import com.woan.api.vo.VadmininfoSimpleList;
import com.woan.api.vo.VloginList;
import com.woan.api.vo.VmessageList;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysAdmininfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.woan.api.model.SysAdmininfo
 * @author MyEclipse Persistence Tools
 */

public class SysAdmininfoDAO extends HibernateDaoSupport  implements SysAdmininfoIDAO  {
	public boolean save(SysAdmininfo sysAdmin)
	{
		try {
			//getHibernateTemplate().merge(sysAdmin);
			getHibernateTemplate().saveOrUpdate(sysAdmin);
			return true;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public SysAdmininfo FindUserByUid(String uid)
	{
		StringBuffer hql = new StringBuffer("from SysAdmininfo where uid='"+uid+"'");
		try
		{
			List<SysAdmininfo> list=this.getHibernateTemplate().find(hql.toString());
			if(list!=null&&list.size()>0)
				return list.get(0);
			else
				return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public SysAdmininfo FindUserByGroupNameAndUsername(String groupname, String username,String userpwd)
	{
		//StringBuffer hql = new StringBuffer("from SysAdmininfo");
		//hql.append(" where  userName='"+username+"' and  higher='"+groupname+"' and  userPwd='"+userpwd+"' and isdel='1'");
		//System.out.println(hql.toString());
		//区分大小写，所以才用以下的方式
		String sql = "select uid,rid,collect_type,nickName as \"nickName\",userName as \"userName\",userPwd as \"userPwd\",createTime as \"createTime\",type,higher,longitude,latitude,province,city,counties,updateTime as \"updateTime\",userClosedate as \"userClosedate\" ,userEmail as \"userEmail\",countrycode,countryname,isdel from sys_admininfo where  userName = '"+username+"' and  higher = '"+groupname+"' and  userPwd='"+userpwd+"'  and isdel='1'";
		System.out.println(sql.toString());
		SysAdmininfo admin = new SysAdmininfo();
		try
		{
			Query query = getSession().createSQLQuery(sql).addEntity(SysAdmininfo.class);
			List<SysAdmininfo> list=query.list();
			if(list!=null&&list.size()>0)
			{
				admin = (SysAdmininfo)list.get(0);
				Hibernate.initialize(admin.getSysRole());
				return admin;
			}
			else
				return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public SysAdmininfo FindUserByGroupNameAndUsername(String groupname, String username)
	{
		StringBuffer hql = new StringBuffer("from SysAdmininfo");
		hql.append(" where userName='"+username+"' and higher='"+groupname+"' and isdel='1'");
		//System.out.println(hql.toString());
		try
		{
			List<SysAdmininfo> list=this.getHibernateTemplate().find(hql.toString());
			if(list!=null&&list.size()>0)
				return list.get(0);
			else
				return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public List<VadmininfoList> FindUserByGroupid(String groupid)
	{
		StringBuffer  buffer=new StringBuffer("SELECT sys_admininfo.uid,sys_admininfo.nickName as \" nickName\" ,sys_admininfo.userName as as \" userName\"," +
				"sys_admininfo.rid,sys_admininfo.userClosedate as \" userClosedate\" ," +
				"sys_admininfo_mobile.deviceNum  as \" deviceNum\",sys_admininfo_mobile.deviceType as \" deviceType\"," +
				"sys_admininfo_mobile.deviceSys as \" deviceSys\",sys_admininfo_mobile.deviceToken as \" deviceToken\",sys_admininfo_mobile.userLicence as \" userLicence\"," +
				"sys_admininfo_mobile.userrules,sys_admininfo_mobile.isauth,sys_admininfo_mobile.userType as \" userType\"  FROM " +
				"sys_admininfo LEFT JOIN sys_admininfo_mobile ON sys_admininfo.uid = sys_admininfo_mobile.uid " +
				"where sys_admininfo.rid='"+groupid+"'");
	
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
				Transformers.aliasToBean(VadmininfoList.class));
		
			List<VadmininfoList> list=query.list();
		
			return list;
		}
		catch(Exception e)
		{
		e.printStackTrace();
		}
		return null;
	}
	public List<VadmininfoSimpleList> FindVGroupUser(String groupid)
	{
		StringBuffer  buffer=new StringBuffer("SELECT uid,userName as \"userName\" from sys_admininfo where rid='"+groupid+"' and type='3'");
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VadmininfoSimpleList.class));
			
			List<VadmininfoSimpleList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
			return null;
	}
	//客户端---一键获取登录信息
	public List<VloginList> FindAdminInfoByDeviceNum(String devicenum)
	{
		StringBuffer  buffer=new StringBuffer("select a.higher,a.userName as \"userName\",b.servicecode from sys_admininfo a,sys_admininfo_mobile b where a.uid = b.uid and b.deviceNum='"+devicenum+"'");
	
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
				Transformers.aliasToBean(VloginList.class));
		
			List<VloginList> list=query.list();
		
			return list;
		}
		catch(Exception e)
		{
		e.printStackTrace();
		}
		return null;
	}
	@Override
	public SysAdmininfo findByUserName(String username) {
		// TODO Auto-generated method stub
		//StringBuffer hql = new StringBuffer("from SysAdmininfo");
		//hql.append(" where userName='"+username+"' and higher='"+groupname+"' and isdel='1'");
		//this.getHibernateTemplate().find("from SysAdmininfo where userName ='"+username+"' ")
		List <SysAdmininfo> syts = this.getHibernateTemplate().find("from SysAdmininfo where userName ='"+username+"' ");
		return syts.get(0);
	}
}